﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using VO.Business.Controllers;
using VO.Console.ServiceReference1;
using VO.Entities;

namespace VO.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            InsertTrackToUser();

            //PopulateDatabase();



            //using (ServiceReference1.TracksServiceClient proxy = new TracksServiceClient())
            //{
            //    List<Track> lst = proxy.GetTracks().ToList();

            //    System.Console.WriteLine("Track count : " + lst.Count);
            //    foreach (Track track in lst)
            //    {
            //        System.Console.WriteLine("Track type : " + track.GetType());
            //        System.Console.WriteLine("Track : " + track.TrackName);
            //    }
            //}
            
            

            //InsertUser();            
            //UpdateUser();
            //InsertClub();
            //UpdateClub();
            //UpdateClubByUser();

            //InsertTracksCategory();
            //InsertTracks();
            //UpdateTrackCategory();
            //UpdateTrack();
            
            //InsertTrackDone();
            //GetUserTracks();

            //InsertBadges();
            //InsertUserBadges();
            //GetUserBadges();


            System.Console.WriteLine("Execution succes!");
        }

        private static void InsertTrackToUser()
        {
            TrackDone td1 = new TrackDone();
            td1.Id = Guid.NewGuid();
            td1.PostedDate = DateTime.Now;
            td1.StartTime = DateTime.Now.AddDays(-2);
            td1.EndTime = DateTime.Now.AddDays(-1).AddHours(2).AddMinutes(15).AddSeconds(24);
            td1.UserBadge = null;
            td1.CompletedTime = (long)td1.EndTime.Subtract(td1.StartTime).TotalSeconds;
            td1.IsValid = true;

            td1.TrackId = new Guid("41F1E0AA-FC8E-4225-AF88-BE9A044E406E");
            td1.UserId = 2;
            //td1.ClubId = tr1.UserOwner.

            TracksDonesController.SaveTrackDone(td1, true);
        }

        private static void PopulateDatabase()
        {
            InsertUser();
            UpdateUser();
            InsertClub();
            InsertClub();
            UpdateClub();
            UpdateClubByUser();

            InsertTracksCategory();
            UpdateTrackCategory();

            InsertTracks();
            InsertTracks();
            InsertTracks();
            UpdateTrack();

            InsertTrackDone();
            InsertTrackDone();
            GetUserTracks();

            InsertBadges();

            InsertUserBadges();
            GetUserBadges();
        }

        private static void GetUserBadges()
        {
            List<UserBadge> userBadges = UsersBadgesController.GetUserBadgesByUserId(1);
            System.Console.WriteLine("User {0} has {1} badges", userBadges[0].UserId, userBadges.Count);
        }

        private static void InsertUserBadges()
        {
            List<Badge> listBadges = BadgesController.GetBadges();
            List<TrackDone> lstTracks = TracksDonesController.GetUserTracksDones(1);

            UserBadge ub1 = new UserBadge();
            ub1.BadgeId = listBadges[0].Id;
            ub1.UserId = 1;
            ub1.EarnedDate = DateTime.Now;
            ub1.TrackDone = lstTracks[0];

            UsersBadgesController.SaveUserBadge(ub1, true);

            UserBadge ub2 = new UserBadge();
            ub2.BadgeId = listBadges[1].Id;
            ub2.UserId = 1;
            ub2.EarnedDate = DateTime.Now;
            ub2.TrackDone = lstTracks[1];

            UsersBadgesController.SaveUserBadge(ub2, true);

        }

        private static void InsertBadges()
        {
            int order = 0;
            Badge b1 = new Badge();
            b1.Id = Guid.NewGuid();
            b1.BadgeName = "Insigna 1";
            b1.BadgeDescription = b1.BadgeName + " " + Guid.NewGuid().ToString();
            b1.OrderId = order++;
            b1.IsSystem = true;

            BadgesController.SaveBadge(b1, true);

            Badge b2 = new Badge();
            b2.Id = Guid.NewGuid();
            b2.BadgeName = "Insigna 2";
            b2.BadgeDescription = b2.BadgeName + " " + Guid.NewGuid().ToString();
            b2.OrderId = order++;
            b2.IsSystem = true;


            BadgesController.SaveBadge(b2, true);

            Badge b3 = new Badge();
            b3.Id = Guid.NewGuid();
            b3.BadgeName = "Insigna 3";
            b3.BadgeDescription = b3.BadgeName + " " + Guid.NewGuid().ToString();
            b3.OrderId = order++;
            b3.IsSystem = true;


            BadgesController.SaveBadge(b3, true);
        }

        private static void GetUserTracks()
        {
            List<TrackDone> doneTracks = TracksDonesController.GetUserTracksDones(1);
            System.Console.WriteLine("User {0} has {1} done tracks", doneTracks[0].UserId, doneTracks.Count);
        }

        private static void InsertTrackDone()
        {
            TrackDone td1 = new TrackDone();
            td1.Id = Guid.NewGuid();
            td1.PostedDate = DateTime.Now;
            td1.StartTime = DateTime.Now.AddDays(-2);
            td1.EndTime = DateTime.Now.AddDays(-1);
            td1.UserBadge = null;
            td1.CompletedTime = 1234;
            td1.IsValid = true;

            Track tr1 = TracksController.GetUserTracks(1)[0];
            td1.TrackId = tr1.Id;
            td1.UserId = tr1.UserId;
            //td1.ClubId = tr1.UserOwner.

            TracksDonesController.SaveTrackDone(td1, true);

        }

        private static void UpdateTrack()
        {
            Track t1 = TracksController.GetUserTracks(1)[0];
            t1.Level=2;
            t1.TrackCategoryId = 2;

            TracksController.SaveTrack(t1, false);
        }

        private static void UpdateTrackCategory()
        {
            TrackCategory tc1 = TracksCategoriesController.GetTrackCategory(1);
            tc1.CategoryName = "category 1 x modified";

            TracksCategoriesController.SaveTrackCategory(tc1, false);
        }

        private static void InsertTracksCategory()
        {
            TrackCategory tc1 = new TrackCategory();
            tc1.CategoryName = "category 1";
            TracksCategoriesController.SaveTrackCategory(tc1, true);

            TrackCategory tc2 = new TrackCategory();
            tc2.CategoryName = "category 2";
            TracksCategoriesController.SaveTrackCategory(tc2, true);

            TrackCategory tc3 = new TrackCategory();
            tc3.CategoryName = "category 3";
            TracksCategoriesController.SaveTrackCategory(tc3, true);
        }

        private static void InsertTracks()
        {
            User u1 = UsersController.GetUser(1);
            System.Console.WriteLine("Inserting tracks for user " + u1.UserDescription);

            Track t1 = new Track();
            t1.Id = Guid.NewGuid();
            t1.Level = 1;
            t1.Locked = false;
            t1.TrackCategoryId = TracksCategoriesController.GetTracksCategories()[0].Id;
            t1.TrackName = "Track Name " + Guid.NewGuid().ToString();

            t1.UserId = u1.Id;

            TracksController.SaveTrack(t1, true);
        }

        private static void UpdateClubByUser()
        {
            User u1 = UsersController.GetUser(1);
            System.Console.WriteLine("Updating " + u1.UserDescription);
            //SerializeType<User>(u1);

            List <Club> lstClubs = u1.ClubsOwned.ToList();
            Club cl1 = lstClubs[0];
            Club cl2 = lstClubs[1];

            cl1.ClubDescription = "second new desc " + Guid.NewGuid().ToString();
            cl2.ClubDescription = "second new desc " + Guid.NewGuid().ToString();

            //System.Console.ReadLine(); System.Console.ReadLine();

            ClubsController.SaveClub(cl1, false);
            ClubsController.SaveClub(cl2, false);

            //System.Console.WriteLine("Succes: " + UserController.SaveUser(u1, false).ToString());

            System.Console.WriteLine("Updated to " + u1.UserDescription);
        }

        private static void SerializeType<T>(T u1)
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            using(FileStream fs = File.Create("tree.txt"))
            {
                ser.Serialize(fs, u1);
            }
        }

        private static void UpdateClub()
        {
            Club u1 = ClubsController.GetClubs().First();
            System.Console.WriteLine("Updating " + u1.ClubDescription);
            u1.ClubDescription = "new clud desc " + Guid.NewGuid().ToString();
            //System.Console.ReadLine(); System.Console.ReadLine();

            ClubsController.SaveClub(u1, false);
            System.Console.WriteLine("Updated to " + u1.ClubDescription);
        }

        private static void InsertClub()
        {
            Club cl1 = new Club();
            cl1.Id = Guid.NewGuid();
            cl1.UserOwnerId = UsersController.GetUsers().First().Id;
            cl1.ClubName = "Club X";
            cl1.ClubDescription = "CLub desc " + Guid.NewGuid().ToString();
            ClubsController.SaveClub(cl1, true);
        }

        private static void UpdateUser()
        {
            User u1= UsersController.GetUser(1);
            System.Console.WriteLine("Updating " + u1.UserDescription);
            u1.UserDescription = "some desc user " + Guid.NewGuid().ToString();
            //System.Console.ReadLine(); System.Console.ReadLine();

            System.Console.WriteLine("Succes: " + UsersController.SaveUser(u1, false).ToString());
            
            System.Console.WriteLine("Updated to " + u1.UserDescription);
        }

        private static void InsertUser()
        {
            User u1 = new User();
            u1.UserDescription = "some desc user " + Guid.NewGuid().ToString();
            u1.IdMembership = Guid.Empty;
            u1.PublicName = "demo user";

            //UserController.GetUsers();
            UsersController.SaveUser(u1, true);
        }
    }
}
